package com.shop2cn.iapi.agentproducts.testcase;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.AddConditionRebateBean;
import com.shop2cn.iapi.agentproducts.parameter.DeleteConditionRebateBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.rebate.ConditionRebateValueDto;
import com.shop2cn.iapi.agentproducts.service.DeleteConditionRebateCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebate;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebateExample;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebateValue;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebateValueExample;
/**
*删除返佣条件Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_DeleteConditionRebate{
	private static DeleteConditionRebateBean deleteconditionrebateBean;
	private static DeleteConditionRebateCall deleteconditionrebateCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("返佣条件详情Test");
	}

	@Before
	public void caseUp() {
		deleteconditionrebateBean=new DeleteConditionRebateBean();
		deleteconditionrebateCall=new DeleteConditionRebateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(deleteconditionrebateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteConditionRebate_001() {
		Logger.start(true,"删除一个条件");
		try {
			Integer mchId = GodmchManageCallService.CreateGodMch();
			Integer agenttype=GodmchManageCallService.getAgentType(mchId, 1);
//			Integer agent_mchId = GodmchManageCallService.getAgentMchId(mchId, agenttype);
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
//			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService
//					.initImportAgentProduct(agent_mchId, "", productId, mchId, false);
//			AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			
			AddConditionRebateBean initAddCondictionRebate = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean);
			
			initAddCondictionRebate.setCatalogRebateList(Arrays.asList(new ConditionRebateValueDto(agenttype,new BigDecimal(20))));

			int RebateId = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate);
			
			deleteconditionrebateBean.setUserId(mchId);
			deleteconditionrebateBean.setConditionRebateId(RebateId);
			deleteconditionrebateCall.setData(deleteconditionrebateBean);
			deleteconditionrebateCall.callService();
			
			Logger.verifyEquals("200", deleteconditionrebateCall.getString("code"), "验证code");
			
			Ymt_ConditionRebateExample example2=new Ymt_ConditionRebateExample();
			example2.createCriteria()
			.andConditionRebateIdEqualTo(RebateId);
			Ymt_ConditionRebate Rebate = SqlMapperFactory.getIntergratedProductMapper().selectYmtConditionRebateExample(example2).get(0);
			
			Logger.verifyEquals(Rebate.getAction(), -1, "验证返佣条件表被删除");
			
			Ymt_ConditionRebateValueExample example3=new Ymt_ConditionRebateValueExample();
			example3.createCriteria()
			.andConditionRebateIdEqualTo(RebateId);
			SqlMapperFactory.getIntergratedProductMapper().selectYmtConditionRebateValueExample(example3).forEach(b->{
				Logger.verifyEquals(b.getAction(), -1, "验证返佣条件值表被删除");
			});;
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteConditionRebate_002() {
		Logger.start(true,"删除一个条件,用户和条件所属人不一致");
		try {
			Integer userId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			AddConditionRebateBean initAddCondictionRebate = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean);
			int RebateId = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate);
			
			
			deleteconditionrebateBean.setUserId(userId+2);
			deleteconditionrebateBean.setConditionRebateId(RebateId);
			deleteconditionrebateCall.setData(deleteconditionrebateBean);
			deleteconditionrebateCall.callService();
			
			Logger.verifyEquals("201", deleteconditionrebateCall.getString("code"), "验证code");
			Logger.verifyEquals("返佣条件不存在", deleteconditionrebateCall.getString("message"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteConditionRebate_003() {
		Logger.start(true,"删除一个条件,条件不存在");
		try {
			Integer userId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			AddConditionRebateBean initAddCondictionRebate = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean);
			int RebateId = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate);
			
			
			deleteconditionrebateBean.setUserId(userId);
			deleteconditionrebateBean.setConditionRebateId(RebateId+1000);
			deleteconditionrebateCall.setData(deleteconditionrebateBean);
			deleteconditionrebateCall.callService();
			
			Logger.verifyEquals("201", deleteconditionrebateCall.getString("code"), "验证code");
			Logger.verifyEquals("返佣条件不存在", deleteconditionrebateCall.getString("message"), "验证code");
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteConditionRebate_004() {
		Logger.start(true,"删除一个已经删除的条件,条件不存在");
		try {
			Integer userId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			AddConditionRebateBean initAddCondictionRebate = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean);
			int RebateId = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate);
			
			
			deleteconditionrebateBean.setUserId(userId);
			deleteconditionrebateBean.setConditionRebateId(RebateId+1000);
			deleteconditionrebateCall.setData(deleteconditionrebateBean);
			deleteconditionrebateCall.callService();
			deleteconditionrebateCall.callService();
			Logger.verifyEquals("201", deleteconditionrebateCall.getString("code"), "验证code");
			Logger.verifyEquals("返佣条件不存在", deleteconditionrebateCall.getString("message"), "验证code");
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteConditionRebate_005() {
		Logger.start(true,"删除一个禁用的条件,成功");
		try {
			Integer userId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			AddConditionRebateBean initAddCondictionRebate = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean);
			int RebateId = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate);
			Thread.sleep(5000);
			AddAgentProductCallService.EnableConditionRebate(userId, RebateId, false);
			Thread.sleep(5000);
			deleteconditionrebateBean.setUserId(userId);
			deleteconditionrebateBean.setConditionRebateId(RebateId);
			deleteconditionrebateCall.setData(deleteconditionrebateBean);
			deleteconditionrebateCall.callService();
			Logger.verifyEquals("200", deleteconditionrebateCall.getString("code"), "验证code");
			
			Ymt_ConditionRebateExample example2=new Ymt_ConditionRebateExample();
			example2.createCriteria()
			.andConditionRebateIdEqualTo(RebateId);
			Ymt_ConditionRebate Rebate = SqlMapperFactory.getIntergratedProductMapper().selectYmtConditionRebateExample(example2).get(0);
			
			Logger.verifyEquals(Rebate.getAction(), -1, "验证返佣条件表被删除");
			
			Ymt_ConditionRebateValueExample example3=new Ymt_ConditionRebateValueExample();
			example3.createCriteria()
			.andConditionRebateIdEqualTo(RebateId);
			SqlMapperFactory.getIntergratedProductMapper().selectYmtConditionRebateValueExample(example3).forEach(b->{
				Logger.verifyEquals(b.getAction(), -1, "验证返佣条件值表被删除");
			});;
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
}